package com.wqiuqiu.gulimall.product.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wqiuqiu.common.constant.ProductConstant;
import com.wqiuqiu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wqiuqiu.gulimall.product.dao.AttrGroupDao;
import com.wqiuqiu.gulimall.product.dao.CategoryDao;
import com.wqiuqiu.gulimall.product.entity.*;
import com.wqiuqiu.gulimall.product.service.*;
import com.wqiuqiu.gulimall.product.vo.AttrResVo;
import com.wqiuqiu.gulimall.product.vo.AttrVo;
import com.wqiuqiu.gulimall.product.vo.RelGroupAndAttrVo;
import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.product.dao.AttrDao;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Autowired
    AttrDao attrDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.save(attrEntity);
        if (attr.getAttrType() == ProductConstant.enumAttr.BASE_ATTR.getCode()){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationDao.insert(relationEntity);
        }
    }


    @Override
    public AttrResVo getAttrById(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrResVo attrResVo = new AttrResVo();
        BeanUtils.copyProperties(attrEntity, attrResVo);
        Long[] paths = categoryService.getCatelogIdPaths(attrEntity.getCatelogId());
        attrResVo.setCatelogPath(paths);
        if (attrEntity.getAttrType() == ProductConstant.enumAttr.BASE_ATTR.getCode()){
            AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq(
                    "attr_id", attrId
            ));
            if (relationEntity != null) {
                attrResVo.setAttrGroupId(relationEntity.getAttrGroupId());
            }
        }
        return attrResVo;
    }

    @Transactional
    @Override
    public void updateAttrVo(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        attrDao.updateById(attrEntity);
        if (attr.getAttrType() == ProductConstant.enumAttr.BASE_ATTR.getCode()){
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            Integer count = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq(
                    "attr_id", attr.getAttrId()
            ));
            attrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrgroupRelationEntity.setAttrId(attr.getAttrId());
            if (count > 0) {
                attrAttrgroupRelationDao.update(attrgroupRelationEntity, new QueryWrapper<AttrAttrgroupRelationEntity>().eq(
                        "attr_id", attr.getAttrId()
                ));

            } else {
                attrAttrgroupRelationDao.insert(attrgroupRelationEntity);
            }
        }
    }

    @Override
    public PageUtils queryAttrPage(Map<String, Object> params, Long categoryId, String type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");

        if (StringUtils.isNotEmpty(key)) {
            wrapper.eq("attr_id", key).or().like("attr_name", key);
        }
        wrapper.and(categoryId != 0, obj->{
            obj.eq("catelog_id", categoryId);
        });
//        if (categoryId != 0) {
//            wrapper.and((obj) -> {
//                obj.eq("catelog_id", categoryId);
//            });
//        }
        int attrType = type.equals("base") ? ProductConstant.enumAttr.BASE_ATTR.getCode() : ProductConstant.enumAttr.SALE_ATTR.getCode();
        wrapper.and((obj) -> {
            obj.eq("attr_type", attrType);
        });
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<AttrResVo> attrResVos = page.getRecords().stream().map(
                (attrEntity) -> {
                    AttrResVo attrResVo = new AttrResVo();
                    BeanUtils.copyProperties(attrEntity, attrResVo);
                    AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationDao.selectOne(
                            new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId())
                    );
                    if (relationEntity != null && relationEntity.getAttrGroupId() != null) {
                        AttrGroupEntity groupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
                        attrResVo.setGroupName(groupEntity.getAttrGroupName());

                    }
                    CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
                    if (categoryEntity != null) {
                        attrResVo.setCatelogName(categoryEntity.getName());
                    }
                    return attrResVo;
                }

        ).collect(Collectors.toList());
        pageUtils.setList(attrResVos);
        return pageUtils;

    }

    @Override
    public List<ProductAttrValueEntity> getSpuAttrInfoBySpuId(Long spuId) {

        List<ProductAttrValueEntity> attrEntities = productAttrValueService.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return attrEntities;
    }

    @Override
    public List<Long> getSearchAttrs(List<Long> ids) {
        List<Long> searchAttrs = baseMapper.getSearchAttrs(ids);
        return searchAttrs;

    }


}